<?

require("header.inc.php");

require("classes.inc.php");
$login = new loginmanager;
$login->verify();

$db = new database_connection;
$db->connect("localhost", "root", "", "roconsult");
$db2 = new database_connection;
$db2->connect("localhost", "root", "", "roconsult");
$db3 = new database_connection;
$db3->connect("localhost", "root", "", "roconsult");
$db4 = new database_connection;
$db4->connect("localhost", "root", "", "roconsult");

if ($_SESSION['userlevel'] == 1)
{ 
  $godmode = true; 
}
else
{ 
  $godmode = false;
}

if ($godmode)
{
  if (!empty($_POST['userid'])){
    $db->query("select `Empnr` from `koppeling` group by `Empnr` having `Empnr` = '".escape($_POST['userid'])."';");
  }else{
    $db->query("select `Empnr` from `koppeling` group by Empnr;");
  }
  
  // Zoek user
  echo "\n<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n";
  echo "<table border=\"1\" id=\"employeesearch\">\n<tr><td>";
  $db2->query("select `Empnr`, `Emp_naam` from `employees`");
  echo "<select name=\"userid\">\n";
  echo "<option value=\"\">Alle Gebruikers</option>\n";
  while ($user = $db2->output()){
    echo "<option value=\"".$user['Empnr']."\">".$user['Emp_naam']."</option>\n";
  }
  echo "</select><br />\n";
  echo "<input type=\"submit\" value=\"Zoeken...\" />";
  echo "\n</td></tr></table>\n</form>";
}
else
{
  $db->query("select `Empnr` from `koppeling` group by Empnr having Empnr = ".$_SESSION['empnr'].";");
}

echo "<table border=\"1\" id=\"gridtable\">\n";
echo "<tr style=\"background: #000; color: #fff;\"><td colspan=\"8\" align=\"center\">ROConsult</td></tr>\n";


while ($employees = $db->output())
{
echo "<tr style=\"background: #C6C6C6;\"><td>Empnr</td><td>Emp_naam</td><td>Projectnr</td><td>Projectnaam</td><td>KostenCode</td><td>Kostenomschrijving</td><td>Datum</td><td>Bedrag</td></tr>\n";
  $db2->query("select `projectnr`, `Empnr` from `koppeling` where Empnr = '".$employees['Empnr']."' group by projectnr;");
  while ($projectnr = $db2->output())
  {
    $db3->query("SELECT koppeling.Empnr, employees.Emp_naam, koppeling.projectnr, projecten.projectnaam, koppeling.kostencode, kosten.kostenomschrijving, koppeling.datum, koppeling.bedrag FROM `koppeling` LEFT JOIN `employees` ON koppeling.Empnr = employees.Empnr LEFT JOIN `projecten` ON koppeling.projectnr = projecten.projectnr LEFT JOIN `kosten` ON koppeling.kostencode = kosten.kostencode WHERE koppeling.projectnr = '".$projectnr['projectnr']."' AND koppeling.Empnr = '".$employees['Empnr']."';");
    while ($regel = $db3->output())
  	{
      echo "<tr>";
      foreach ($regel as $key => $kolom)
      {
        if ($key == "bedrag"){
          echo "<td style=\"text-align: right;\">€ ";
        }else{
          echo "<td>";
        }
        echo $kolom;
        echo "</td>";
      }      
      echo "</tr>\n";
  	}
    $db4->query("SELECT SUM( koppeling.bedrag ) AS `subtotaal` , koppeling.projectnr, koppeling.Empnr
FROM `koppeling` WHERE koppeling.projectnr = '".$projectnr['projectnr']."' AND koppeling.Empnr = '".$employees['Empnr']."' GROUP BY koppeling.projectnr;");
    $subtotaal = $db4->output();
    echo "<tr style=\"background: #E2E2E2;\"><td colspan=\"5\"></td><td>subtotaal</td><td></td><td style=\"text-align:right;\">€ ".$subtotaal['subtotaal']."</td></tr>\n";
  }
  
  $db4->query("select SUM(koppeling.bedrag) AS totaal, koppeling.Empnr FROM `koppeling` LEFT JOIN `kosten` ON koppeling.kostencode = kosten.kostencode GROUP BY koppeling.Empnr HAVING koppeling.Empnr = '".$employees['Empnr']."';");
  $totaal = $db4->output();
  echo "<tr style=\"background: #E9E9E9;\"><td colspan=\"5\"></td><td>TOTAAL</td><td></td><td style=\"text-align:right;\">€ ".$totaal['totaal']."</td></tr>";
  }
echo "</table>";

// Free resultset & close link
$db->closelink();
$db2->closelink();
$db3->closelink();
$db4->closelink();

require("footer.inc.php");
?>